Lab5


4531206721_4531207321  นาย เฉลิมพงศ์ สัตยาวิบูล และ นาย ชัยชนะ นิลวัชรารัง (13/8/2545 (11:30:06))
(SM=2, CM=29, ST=40, KY=0, TR=12:03)

TestScript
Mini-Quiz :  (0.0 คะแนน)

JLab>javac Lab5.java
JLab>
JLab>java Selftest
>>JLabIO->Testing (2009, 2) : 2.00
>>JLabIO->Testing (2023, 9) : 2.00
>>JLabIO->Testing (2006, 2) : 2.00
>>JLabIO->Testing (2003, 3) : 2.00
>>JLabIO->Testing (1994, 8) : 2.00

>>JLab:<POINT>10.00</POINT>
JLab>

ได้ 10 คะแนน
Source Code
/**
 * 2110101 Computer Programming
 * Lab #5 : Loops : while, do-while, for
 */
import java.util.*;
import jlab.JLabIO;

public class Lab5 {
  public static void calendar(int year, int month) {
    // Example
    //-----------------------------
    //        August(2002)
    //-----------------------------
    // SUN MON TUE WED THU FRI SAT
    //                   1   2   3     \
    //   4   5   6   7   8   9  10      |
    //  11  12  13  14  15  16  17       >  your code
    //  18  19  20  21  22  23  24      |
    //  25  26  27  28  29  30  31     /
    //=============================
    
    // print header
    System.out.println(LONGLINE);
    System.out.println("        " + monthName[month - 1] + "(" + year + ")");
    System.out.println(LONGLINE);
    System.out.println(" SUN MON TUE WED THU FRI SAT");
    
    Calendar calendar = new GregorianCalendar(new Locale("th", "TH"));
    calendar.set((year > 2500 ? year - 543 : year), month - 1, 1);
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    int numDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    
    // add your code here to format and print the month
    // Use the following two variables :
    //   - numDays is the number of days in this month
    //   - firstDay is the day-of-week of the first of this month
    

int Seven = firstDay;
for (int a = 1; a < firstDay; a++) {
  System.out.print("    "); }
for (int a = 1; a <= numDays; a++) {

 System.out.print(JLabIO.format(a, 4));

 if (Seven % 7 == 0) {System.out.println();}

 Seven++;
  }
System.out.println();



    
    // add a line to notify the end of printing
    System.out.println(LASTLINE);
  }
  
  //----------------------------------------------------------------
  public static void main(String[] args) {
    int y = JLabIO.readInt("Year = ");
    int m = JLabIO.readInt("Month = ");

    if (y > 0 && (1 <= m && m <= 12)) {
      calendar(y, m);
    } else {
      System.out.println("error : invalid year or month");
    }
  }
  //----------------------------------------------------------------
  
  final static String[] monthName =
     {"January", "February", "March", "April",
      "May", "June", "July", "August",
      "September", "October", "November", "December"};
  final static String LONGLINE = "-----------------------------";
  final static String LASTLINE = "=============================";
  
}